﻿using CoreModule;
using Microsoft.Extensions.Logging;
using Prism.Ioc;
using Prism.Mvvm;
using Prism.Regions;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Input;
namespace IdResourceModule.ViewModels
{
    /// <summary>
    /// 通用配置界面
    /// </summary>
    public class ConfigurationHomeViewModel : BindableBase
    {
       
        /// <summary>
        /// 
        /// </summary>
        public FunctionConfigurationInfo FunctionConfigurationInfo { get; set; } = new FunctionConfigurationInfo();
        public SeriesPortConfigurationInfo SeriesPortConfigurationInfo { get; set; } = new SeriesPortConfigurationInfo();
        public List<DeviceConfigurationInfo> RecognitionDeviceConfigurationInfos { get; set; } = new List<DeviceConfigurationInfo>();
        public List<DeviceConfigurationInfo> HisDeviceConfigurationInfos { get; set; } = new List<DeviceConfigurationInfo>();
        IConfigurationManager ConfigurationManager { get; set; }
        IDeviceManager DeviceManager { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string RecognitionDeviceName { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string HisDeviceName { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string MeasurementDeviceName { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public Visibility HisDeviceConfigurationInfosVisibility { get; set; } = Visibility.Visible;
        public Visibility RecognitionDeviceConfigurationInfosVisibility { get; set; } = Visibility.Visible;
        public Visibility MeasurementTimeoutVisibility { get; set; } = Visibility.Visible;
        IContainerProvider ContainerProvider { get; set; }
        ILogger Logger { get; set; }
        IWindowLifetime WindowLifetime { get; set; }
        IRegionManager RegionManager { get; set; }
        INotifier Notifier { get; set; }    
        /// <summary>
        /// 
        /// </summary>
        public ConfigurationHomeViewModel(
            INotifier notifier,
            IRegionManager regionManager,
            IConfigurationManager configurationManager,
            IDeviceManager deviceManager,
            IContainerProvider containerProvider,
            IWindowLifetime windowLifetime,
             ILogger logger)
        {
            Notifier= notifier;
            RegionManager = regionManager;
            ConfigurationManager = configurationManager;
            DeviceManager = deviceManager;
            ContainerProvider = containerProvider;
          
            WindowLifetime= windowLifetime;
            Logger = logger;
            
            SeriesPortConfigurationInfo.Load();
            RecognitionDeviceConfigurationInfos.Clear();
             
            HisDeviceConfigurationInfos
            .AddRange(ConfigurationManager.HisDeviceConfigurationInfos);
             
            
              RecognitionDeviceConfigurationInfos
                 .AddRange(ConfigurationManager
                 .RecognitionDeviceConfigurationInfos
                 .Where(item => ContainerProvider.IsRegistered<IRecognitionDevice>(item.DeviceName)));
         
          
              
           
           
            var hisModuleConfigurationInfo = new DeviceConfigurationInfo();
            hisModuleConfigurationInfo.Load(DeviceConfigurationInfo.HisDeviceConfigurationInfoFilePath);
            HisDeviceName = hisModuleConfigurationInfo.DeviceName;
            if (string.IsNullOrWhiteSpace(HisDeviceName)  && HisDeviceConfigurationInfos.Count>0)
            {
                HisDeviceName = HisDeviceConfigurationInfos.FirstOrDefault().DeviceName;
            }
             
            Logger.LogInformation("ConfigurationHomeViewModel");
        }
        /// <summary>
        /// 
        /// </summary>
        public ICommand SaveConfigurationCommand => new Prism.Commands.DelegateCommand<Window>(SaveConfiguration);
        /// <summary>
        /// 
        /// </summary>
        public void SaveConfiguration(Window window)
        {
            Logger.LogInformation("begin SaveConfiguration");
            FunctionConfigurationInfo.Save();
            SeriesPortConfigurationInfo.Save();
            ConfigurationManager.SeriesPortConfigurationInfo = SeriesPortConfigurationInfo;
            ConfigurationManager.FunctionConfigurationInfo = FunctionConfigurationInfo;
            ConfigurationManager.RecognitionDeviceConfigurationInfos.Clear();
            ConfigurationManager.RecognitionDeviceConfigurationInfos.AddRange(this.RecognitionDeviceConfigurationInfos);
            ConfigurationManager.RecognitionDeviceConfigurationInfos.Save();
            DeviceManager.StopMeasurementDevices();
            var seriesPortMeasurementDevice = ContainerProvider.Resolve<IMeasurementDevice>(CoreConstant.SeriesPortMeasurementDeviceManager);
            DeviceManager.AddMeasurementDevice(seriesPortMeasurementDevice);
            DeviceManager.StartMeasurementDevices();
            DeviceManager.StopRecognitionDevices();
            DeviceManager.StartRecognitionDevices();
            if (!string.IsNullOrWhiteSpace(HisDeviceName)
                && ContainerProvider.IsRegistered<IHisDevice>(HisDeviceName))
            {
                DeviceManager.StopHisDevices();
                var hisDevice = ContainerProvider.Resolve<IHisDevice>(HisDeviceName);
                DeviceManager.AddHisDevice(hisDevice);
                DeviceManager.StartHisDevices();
            }
            var hisModuleConfigurationInfo = new DeviceConfigurationInfo();
            hisModuleConfigurationInfo.DeviceName = HisDeviceName;
            hisModuleConfigurationInfo.Save(DeviceConfigurationInfo.HisDeviceConfigurationInfoFilePath);
            Logger.LogInformation("end SaveConfiguration");
          
             
            window.Close();

            var String_Save_System_Configuration_Successfully = WPFLocalizeExtension.Engine.LocalizeDictionary.Instance.GetLocalizedObject("String_Save_System_Configuration_Successfully", null, null) + "";
             
            Notifier.ShowSuccess(String_Save_System_Configuration_Successfully);
        }
    }
}
